package com.yx.yinshanjinniu.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.utils.TimeCount;


/**
 * Created by Mr on 2018/7/16.
 */

public class MyEditText extends RelativeLayout implements TextWatcher, CompoundButton.OnCheckedChangeListener, View.OnClickListener, View.OnFocusChangeListener {
    private EditText et_content;
    private CheckBox cb_eyes;
    private ImageView iv_clear;
    private View view_bottom_line;
    private TextView tv_title;

    private boolean isPwdEditText;

    public MyEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.view_my_edit_text, this, true);
        findViews(view);
        setListeners();
        init(context, attrs);
    }

    private void setListeners() {
        et_content.addTextChangedListener(this);
        et_content.setOnFocusChangeListener(this);
        cb_eyes.setOnCheckedChangeListener(this);
        iv_clear.setOnClickListener(this);
    }

    private void findViews(View view) {
        et_content = view.findViewById(R.id.et_content);
        tv_title = view.findViewById(R.id.tv_title);
        cb_eyes = view.findViewById(R.id.cb_eyes);
        iv_clear = view.findViewById(R.id.iv_clear);
        view_bottom_line = view.findViewById(R.id.view_bottom_line);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyEditText);
        String hint = array.getString(R.styleable.MyEditText_hint);
        String title = array.getString(R.styleable.MyEditText_title);
        int maxLength = array.getInt(R.styleable.MyEditText_maxLength, 0);
        int drawableLeft = array.getResourceId(R.styleable.MyEditText_drawableLeft, 0);
        int inputType = array.getInt(R.styleable.MyEditText_inputType, 0);
        boolean bottomLine = array.getBoolean(R.styleable.MyEditText_bottomLine, true);
        if (hint != null) {
            et_content.setHint(hint);
        }
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
            tv_title.setVisibility(VISIBLE);
        }else{
            tv_title.setVisibility(GONE);
        }
        if (maxLength != 0) {
            et_content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }
        if (bottomLine) {
            view_bottom_line.setVisibility(VISIBLE);
        } else {
            view_bottom_line.setVisibility(GONE);
        }
        et_content.setCompoundDrawablesWithIntrinsicBounds(drawableLeft, 0, 0, 0);
        switch (inputType) {
            case 0:
                et_content.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case 1:
                et_content.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 2:
                et_content.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                break;
            case 3:
                et_content.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                break;
            case 4:
                et_content.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD | InputType.TYPE_CLASS_NUMBER);
                break;
        }
        isPwdEditText = (inputType == 3 || inputType == 4);
        array.recycle();

        cb_eyes.setVisibility(GONE);
        iv_clear.setVisibility(GONE);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.length() > 0) {
            if (isPwdEditText) {
                cb_eyes.setVisibility(VISIBLE);
            }
            iv_clear.setVisibility(VISIBLE);
        } else {
            cb_eyes.setVisibility(GONE);
            iv_clear.setVisibility(GONE);
        }
        if (null != mWatcher) {
            mWatcher.onAfterText(getId(), s.toString());
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus && et_content.getText().length() > 0) {
            if (isPwdEditText) {
                cb_eyes.setVisibility(VISIBLE);
            }
            iv_clear.setVisibility(VISIBLE);
        } else {
            cb_eyes.setVisibility(GONE);
            iv_clear.setVisibility(GONE);
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            et_content.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_CLASS_TEXT);// 输入为密码且可见
        } else {
            et_content.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);// 设置文本类密码（默认不可见），这两个属性必须同时设置
        }
        et_content.setSelection(et_content.getText().length());
    }

    @Override
    public void onClick(View v) {
        et_content.setText("");
    }

    public void setText(CharSequence text) {
        et_content.setText(text);
    }

    public void setEnable(boolean enable) {
        et_content.setEnabled(enable);
        if(enable){
            iv_clear.setVisibility(VISIBLE);
        }else{
            iv_clear.setVisibility(GONE);
        }
    }

    public void setShowDeleteBtn(int visible){
        iv_clear.setVisibility(visible);
    }

    public void setHint(CharSequence text) {
        et_content.setHint(text);
    }

    public void setText(int resId) {
        et_content.setText(resId);
    }

    public String getText() {
        return et_content.getText().toString().trim();
    }

    public void showSoftKeyboard() {
        et_content.setFocusable(true);
        et_content.setFocusableInTouchMode(true);
        et_content.requestFocus();
        new TimeCount(300, 300) {
            @Override
            public void onFinish() {
                InputMethodManager inputManager = (InputMethodManager) et_content.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(et_content, 0);
            }
        }.start();
    }

    public AfterTextWatcher mWatcher;

    public void addTextWatcher(AfterTextWatcher watcher) {
        this.mWatcher = watcher;
    }

    public interface AfterTextWatcher {
        void onAfterText(int vId, String text);
    }
}
